<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.username" placeholder="名称" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit" v-hasPermi="['system:sysUser:add']">新增</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table border v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip align="center" min-width="50" type="selection"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="account" label="登录账号"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="userCode" label="工号"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="userName" label="名称"></el-table-column>
      <el-table-column align="center" min-width="80" prop="gender" label="性别">
        <template #default="{ row }">
          <el-tag v-if="row.gender == '1'">男</el-tag>
          <el-tag v-else-if="row.gender == '0'">女</el-tag>
          <el-tag v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="phone" label="手机号码"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="email" label="邮箱"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="deptName" label="所属部门"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="posts" label="所属岗位">
        <template #default="{ row }">
          <el-tooltip effect="dark" content="查看所属岗位" placement="top">
            <el-button icon="el-icon-view" type="text" @click="handlePost(row)">查看</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="roles" label="所属角色">
        <template #default="{ row }">
          <el-tooltip effect="dark" content="查看所属角色" placement="top">
            <el-button icon="el-icon-view" type="text" @click="handleRole(row)">查看</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="createdBy" label="创建人"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="160" prop="createdTime" label="创建时间"></el-table-column>
      <el-table-column align="center" label="操作" width="150">
        <template #default="{ row }">
          <el-tooltip effect="dark" content="编辑当前行" placement="top">
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              circle
              @click="handleEdit(row)"
              v-hasPermi="['system:sysUser:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="重置密码" placement="top">
            <el-button
              icon="el-icon-refresh-right"
              type="warning"
              size="mini"
              circle
              @click="handleResetPwd(row)"
              v-hasPermi="['system:sysUser:ressetPwd']"
            ></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除当前行" placement="top">
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="mini"
              circle
              @click="handleDelete(row)"
              v-hasPermi="['system:sysUser:delete']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <role ref="role" @fetch-data="fetchData"></role>
    <post ref="post" @fetch-data="fetchData"></post>
  </div>
</template>

<script>
  import { getList, doDelete, doResetPwd } from '@/api/system/user'
  import Edit from './components/Edit'
  import Role from './components/Role'
  import Post from './components/Post'

  export default {
    name: 'User',
    components: { Edit, Role, Post },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: undefined,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handlePost(row) {
        if (row.userId) {
          this.$refs['post'].showEdit(row.userId)
        }
      },
      handleRole(row) {
        if (row.userId) {
          this.$refs['role'].showEdit(row.userId)
        }
      },
      handleEdit(row) {
        if (row.userId) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleResetPwd(row) {
        if (row) {
          this.$baseConfirm('你确定要重置该用户密码吗', null, async () => {
            await doResetPwd(row.userId).then((res) => {
              if (res.success) {
                this.$baseMessage(res.msg, 'success')
                this.fetchData()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleDelete(row) {
        if (row) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await doDelete(row.userId).then((res) => {
              if (res.success) {
                this.$baseMessage(res.msg, 'success')
                this.fetchData()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.list
        this.total = data.total
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
